<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scll:form:edit']"
        >领料</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scll:form:edit']"
        >查看</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="scllId" />
      <el-table-column label="领料单号" align="center" prop="lldh" />
      <el-table-column label="领料单状态" align="center" prop="lldState" :formatter="isStatus"/>
      <el-table-column label="生产工单编码" align="center" prop="gdCode" />
      <el-table-column label="负责人" align="center" prop="principal" />
      <el-table-column label="申请人" align="center" prop="proposer" />
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scll:form:edit']"
          >领料</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scll:form:remove']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产领料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="领料单号" prop="lldh">
          <el-input v-model="form.lldh" placeholder="请输入领料单号" />
        </el-form-item>
        <el-form-item label="领料单状态 1.已经领料 0.未领料" prop="lldState">
          <el-input v-model="form.lldState" placeholder="请输入领料单状态 1.已经领料 0.未领料" />
        </el-form-item>
        <el-form-item label="生产工单编码" prop="gdCode">
          <el-input v-model="form.gdCode" placeholder="请输入生产工单编码" />
        </el-form-item>
        <el-form-item label="负责人" prop="principal">
          <el-input v-model="form.principal" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="申请人" prop="proposer">
          <el-input v-model="form.proposer" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applicationTime">
          <el-date-picker clearable
            v-model="form.applicationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">生产领料所需物料信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddScllMaterialRequiredRescord">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteScllMaterialRequiredRescord">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="scllMaterialRequiredRescordList" :row-class-name="rowScllMaterialRequiredRescordIndex" @selection-change="handleScllMaterialRequiredRescordSelectionChange" ref="scllMaterialRequiredRescord">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物料编码" prop="wlCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.wlCode" placeholder="请输入物料编码" />
            </template>
          </el-table-column>
          <el-table-column label="所需物料数量" prop="getNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.getNum" placeholder="请输入所需物料数量" />
            </template>
          </el-table-column>
          <el-table-column label="仓库编码" prop="ckCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ckCode" placeholder="请输入仓库编码" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listForm, getForm, delForm, addForm, updateForm } from "@/api/scll/form";

export default {
  name: "Form",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedScllMaterialRequiredRescord: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产领料表格数据
      formList: [],
      // 生产领料所需物料表格数据
      scllMaterialRequiredRescordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lldh: null,
        lldState: null,
        gdCode: null,
        principal: null,
        proposer: null,
        applicationTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lldh: [
          { required: true, message: "领料单号不能为空", trigger: "blur" }
        ],
        lldState: [
          { required: true, message: "领料单状态 1.已经领料 0.未领料不能为空", trigger: "blur" }
        ],
        gdCode: [
          { required: true, message: "生产工单编码不能为空", trigger: "blur" }
        ],
        principal: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        proposer: [
          { required: true, message: "申请人不能为空", trigger: "blur" }
        ],
        applicationTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产领料列表 */
    getList() {
      this.loading = true;
      listForm(this.queryParams).then(response => {
        this.formList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     * 判断领料状态
     * */
    isStatus(row, column, cellValue){
      if (cellValue=="1"){
        return "已领料";
      }else{
        return "未领料";
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scllId: null,
        lldh: null,
        lldState: null,
        gdCode: null,
        principal: null,
        proposer: null,
        applicationTime: null
      };
      this.scllMaterialRequiredRescordList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scllId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产领料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scllId = row.scllId || this.ids
      getForm(scllId).then(response => {
        this.form = response.data;
        this.scllMaterialRequiredRescordList = response.data.scllMaterialRequiredRescordList;
        this.open = true;
        this.title = "修改生产领料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.scllMaterialRequiredRescordList = this.scllMaterialRequiredRescordList;
          if (this.form.scllId != null) {
            updateForm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addForm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scllIds = row.scllId || this.ids;
      this.$modal.confirm('是否确认删除生产领料编号为"' + scllIds + '"的数据项？').then(function() {
        return delForm(scllIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 生产领料所需物料序号 */
    rowScllMaterialRequiredRescordIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 生产领料所需物料添加按钮操作 */
    handleAddScllMaterialRequiredRescord() {
      let obj = {};
      obj.wlCode = "";
      obj.getNum = "";
      obj.ckCode = "";
      this.scllMaterialRequiredRescordList.push(obj);
    },
    /** 生产领料所需物料删除按钮操作 */
    handleDeleteScllMaterialRequiredRescord() {
      if (this.checkedScllMaterialRequiredRescord.length == 0) {
        this.$modal.msgError("请先选择要删除的生产领料所需物料数据");
      } else {
        const scllMaterialRequiredRescordList = this.scllMaterialRequiredRescordList;
        const checkedScllMaterialRequiredRescord = this.checkedScllMaterialRequiredRescord;
        this.scllMaterialRequiredRescordList = scllMaterialRequiredRescordList.filter(function(item) {
          return checkedScllMaterialRequiredRescord.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleScllMaterialRequiredRescordSelectionChange(selection) {
      this.checkedScllMaterialRequiredRescord = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scll/form/export', {
        ...this.queryParams
      }, `form_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
